package com.lsq.wuyuge.service.impl;

import com.lsq.wuyuge.context.BaseContext;
import com.lsq.wuyuge.dto.ArticleInfoDTO;
import com.lsq.wuyuge.dto.ArticlePublishDTO;
import com.lsq.wuyuge.entity.Article;
import com.lsq.wuyuge.mapper.ArticleMapper;
import com.lsq.wuyuge.mapper.UserMapper;
import com.lsq.wuyuge.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author 一声闷雷
 * @since 2025-03-15
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private UserMapper  userMapper;

    /**
     * 创建文章
     * @param articlePublishDTO
     * @return
     */
    @Override
    public ArticleInfoDTO addArticle(ArticlePublishDTO  articlePublishDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(articlePublishDTO,article);
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setUserId(BaseContext.getCurrentId());
        articleMapper.insert(article);
        ArticleInfoDTO articleInfo = new ArticleInfoDTO();
        BeanUtils.copyProperties(article,articleInfo);
        articleInfo.setUsername(userMapper.selectById(article.getUserId()).getUsername());
        return articleInfo;
    }
}
